package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;

/**
 * 标样核查服务
 * @author Administrator
 *
 */
@Service
public class StandardMaterialReplaceService {

	/**
	 * 查看标样核查记录，用于前端数据展示和报表导出
	 * @param params
	 * @param pTable
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<RowBase> getStandardMaterialReplaceList(Map<String, String> params,ITable pTable, PageInfo pageInfo) throws Exception {
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		String sql = 
				"select t1.*, t2.c_userealname as username, t3.name as station_name\r\n" + 
				"  from operation_standard_replace t1\r\n" + 
				"  left join safe_user t2 on t1.user_id = t2.s_userid\r\n" + 
				"  left join BC_SITE_INFO t3 on t3.code = t1.station_id\r\n" + 
				" where 1 = 1\r\n";

		if (params.containsKey("task_code")) {
			sql += " and t1.TASK_ID = @task_code";
			listParam.add(new GlobalParameter("task_code", params.get("task_code")));
		} 
		if (params.containsKey("station_id")) {
			sql += " and t1.STATION_ID = @station_id";
			listParam.add(new GlobalParameter("station_id", params.get("station_id")));
		} 

		sql = sql + " order by t1.MAKE_TIME desc ";
		return DataUtil.getPageRow(sql, listParam, pTable, pageInfo);
	}   
	
	/**
	 * 统计该站点下，标准物质更换的仪器名称有多少个，用于xml中循环
	 * @param params
	 * @param pTable
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<RowBase> getReplaceInstrutStatisticsList(Map<String, String> params,ITable pTable) throws Exception {
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		String sql = 	"select t.device_name,t.PARAM,t.device_type  from operation_standard_replace t where 1=1";

		if (params.containsKey("task_code")) {
			sql += " and t.TASK_ID = @task_code";
			listParam.add(new GlobalParameter("task_code", params.get("task_code")));
		} 
		if (params.containsKey("station_id")) {
			sql += " and t.STATION_ID = @station_id";
			listParam.add(new GlobalParameter("station_id", params.get("station_id")));
		}  

		sql = sql + " group by t.device_name,t.param,t.device_type ";
		return DataUtil.getSelectRows(sql, listParam, pTable);
	}   

}
